package GDoc::Job::Fetch::US::Legi::CBO::Briefs;

use Moose;

use WWW::Mechanize;
use GDoc::Job;
use File::Temp;
use DateTime::Format::Natural;

extends 'GDoc::Job::Fetch::US::Legi::CBO';

has '_dt'      => ( is       => 'ro',
                    init_arg => undef,
                    default  => sub { DateTime::Format::Natural->new }
                  );

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://www.cbo.gov/publications/bydoctype.cfm?dtype=10' );
    $self->_basedir( 'http://www.cbo.gov' );
}

sub run { 
    my $self = shift;
   
    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );
        my $html = $mech->response->content;

        my @lis = ( $html =~ m{<li class="pubsbrowse">(.+?)</li>}gsi );

        foreach my $li( @lis ) { 
            $li =~ m{<A HREF="(?<url>/doc\.cfm\?index=\d+)">(?<title>.+?)</A>.+?<br>(?<date>\w+\s\d{1,2},\s\d{4})}gsi;
            my ( $url, $title, $date ) = @+{'url', 'title', 'date'};
            next unless $url and $title and $date;

            $url .= '&type=1';   # gets us the PDF version
            my $job = GDoc::Job->new( class => 'Fetch::US::Legi::CBO::Briefs',
                                      queue => $self->fetch_queue,
                                      data  => { link    => $url,
                                                 desc    => $title,
                                                 date    => $date
                                               }
                                    );

            $job->submit;
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    my $date = $self->_dt->parse_datetime( $data{date} );
    my $ident = sprintf( 'CBO-B-%04d-%02d-%02d', $date->year, $date->month, $date->day );
    my $filename = $ident . '.pdf';
    my $key = 'us/legi/cbo/' . $filename;

    $self->logger->info( "Storing $data{link} --> $key" );

    my $mech = $self->_mech;
    my $tmpf = File::Temp->new;
    $mech->get( $self->_basedir . $data{link}, ':content_file' => $tmpf->filename );

    $self->store_pdf( key      => $key,
                      datafile => $tmpf,
                      filename => $filename,
                      meta     => { description   => $data{desc},
                                    identifier    => $ident,
                                  }
                    );

}



1;
